//
//  WLMCompose.swift
//  WeiBo
//
//  Created by Eric on 2017-01-12.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit
import pop

class WLMComposeView: UIView {
    
    
    var belongVc: UIViewController?
    
    lazy var buttonArray: [WLMComposeButton] = [WLMComposeButton]()
    lazy var backView: UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyTintEffect(with: #colorLiteral(red: 0.7255115799, green: 0.7255115799, blue: 0.7255115799, alpha: 1)))

    //展现撰写视图
    func show(fromVC: UIViewController?) {
        
        self.belongVc = fromVC
        //传递控制器
        fromVC?.view.addSubview(self)

    }
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        let backImageView = UIImageView (image: #imageLiteral(resourceName: "compose_slogan"))
        self.addSubview(backView)
        backView.addSubview(backImageView)
        backImageView.snp.makeConstraints { (make) in
            make.top.equalTo(backView).offset(50)
            make.centerX.equalTo(backView)
            make.width.equalTo(200)
            make.height.equalTo(70)
        }
        let imageNameArr = ["tabbar_compose_idea",
                            "tabbar_compose_photo",
                            "tabbar_compose_shooting",
                            "tabbar_compose_music",
                            "tabbar_compose_friend",
                            "tabbar_compose_more"]
        let titleArray = ["文字", "照片/视频", "秒拍", "音乐", "好友圈", "更多"]
        
        addComposeBtns(image: imageNameArr, title: titleArray)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (index,btn) in buttonArray.reversed().enumerated() {
            self.startAnimation(btn: btn, index: index,isShow: false)
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { 
            
            self.removeFromSuperview()
        }
    }
    
    private func startAnimation(btn: WLMComposeButton, index: Int, isShow: Bool = true) {
        //添加动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置属性
        springAnimation?.springBounciness = 10.0
        springAnimation?.springSpeed = 10
        //设置延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350 : 350)
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        btn.pop_add(springAnimation, forKey: nil)
    }
    
    private func addComposeBtns(image:Array<String>, title:Array<String>){
        let btnMargin = (ScreenWidth - 3 * composeBtnImageWidth) / 4
        
        for i in 0..<6 {
            let btn = WLMComposeButton()
            let coloum = i % 3
            let row = i / 3
            let btnX = btnMargin + (composeBtnImageWidth + btnMargin) * CGFloat(coloum)
            let btnY = (composeBtnHeight + btnMargin) * CGFloat(row) + ScreenHeight
            btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnImageWidth, height: composeBtnHeight)
            btn.setImage(UIImage(named: image[i]), for: .normal)
            btn.setTitle(title[i], for: .normal)
            btn.setTitleColor(UIColor.randomFlat, for: .normal)
            self.addSubview(btn)
            self.buttonArray.append(btn)
            btn.addTarget(self, action: #selector(composeBtnDidClick(btn:)), for: .touchUpInside)
        }
    }
    
    @objc private func composeBtnDidClick(btn: WLMComposeButton) {
        UIView.animate(withDuration: 0.5, animations: {
            for composeBtn in self.buttonArray {
                composeBtn.alpha = 0.1
                if composeBtn == btn {
                    composeBtn.transform = CGAffineTransform(scaleX: 2, y: 2)
                }
                else {
                    composeBtn.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                }
            }
        }, completion: { (_) -> () in
            let classStr = "WeiBo.WLMComposeViewController"
            let classType = NSClassFromString(classStr) as! UIViewController.Type
            let composeVC = classType.init()
            let naviC = UINavigationController(rootViewController: composeVC)
            self.belongVc?.present(naviC, animated: true, completion: {
                self.removeFromSuperview()
            })
        })
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        for (offset, btn) in buttonArray.enumerated() {
            self.startAnimation(btn: btn, index: offset)
        }
    }
}










